<div class="layui-card">

    <div class="layui-card-body">
        <div class="layui-form toolbar">
            <div class="layui-inline layui-show-xs-block">
                <input class="layui-input" autocomplete="off" placeholder="活动日期" name="start" id="start"></div>
            <button id="menus-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="menus-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="platform-table" lay-filter="menus-table"></table>
    </div>
</div>

<!--状态操作-->
<script type="text/html" id="switchTpl">
    <!-- 这里的checked的状态只是演示 -->
    <input type="checkbox" name="enable" value="{{d.actiid}}" lay-skin="switch" lay-text="关闭|开启" lay-filter="off"
           {{d.enable=='1'?'checked':''}}>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="order-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>

    layui.use(['form', 'table', 'util', 'config', 'admin', 'formSelects', 'treetable','layer','laydate'],function () {
        var table = layui.table;
        var layer = layui.layer;
        var admin = layui.admin;
        var treetable = layui.treetable;
        var config = layui.config;
        var form=layui.form;
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });
        // 渲染表格
       function aa(){
           table.render({
               elem: '#platform-table',
               url: config.base_server + 'api-administrators/platformActivity/findAll',
               where: {
                   access_token: config.getToken().access_token,
               },
               page: true,
               parseData: function (res) {
                   console.log(res)
                   return {
                       "code": 0,
                       "msg": "",
                       "total": res.data.total,
                       "data": res.data.activity,
                       "count": res.data.count,
                   };
               },
               cols: [[
                   {type: 'numbers'},
                   {field: 'reduce', minWidth: 200, title: '减免金额'},
                   {field: 'remark', title: '活动详情'},
                   {field: 'crtime', title: '活动创建'},
                   {field: 'enable', width: 95, templet: '#switchTpl', title: '活动状态'},
                   {templet: '#order-state', width: 120, align: 'center', title: '操作'}
               ]]
           });
       }
       aa();
        //监听状态操作
        form.on('switch(off)', function (obj) {
            if (obj.othis[0].innerText == '关闭') {
                $.ajax({
                    url: config.base_server + 'api-administrators/platformActivity/update',//地址：访问后台的地址
                    data: {actiid: obj.value,enable: 0, access_token: config.getToken().access_token,},//前端传递给后台的数据
                    type: 'post',
                    dataType: 'json',//是后端传递到前端的数据格式
                    success: function (data) {//后端给前端的一个反馈
                        layer.msg('开启成功！')
                    }
                });
            }else {
                $.ajax({
                    url: config.base_server + 'api-administrators/platformActivity/update',//地址：访问后台的地址
                    data: {actiid: obj.value,enable: 1, access_token: config.getToken().access_token,},//前端传递给后台的数据
                    type: 'post',
                    dataType: 'json',//是后端传递到前端的数据格式
                    success: function (data) {//后端给前端的一个反馈
                        layer.msg('关闭成功！')
                    }
                });
            }

        });
        // 工具条点击事件
        table.on('tool(menus-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') {
                layer.confirm('确定删除此菜单吗？', function () {
                    layer.load(2);

                    admin.req('api-administrators/order/delete', {id:obj.data.ordeid}, function (data) {
                        layer.closeAll('loading');
                        if (0 == data.code) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            order();
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'post');

                });
            }
        });
        // 显示编辑弹窗
        var showEditModel = function (data) {
            // debugger;
            // if (data) {
            //     data.isMenu = data.isMenu.toString();
            //     data.hidden = data.hidden.toString();
            // }
            var title = data ? '修改活动' : '添加活动';
            admin.putTempData('t_menus', data);

            admin.popupCenter({
                title: title,
                path: 'pages/activity/plat_form.html',
                finish: function () {
                    aa();
                }
            });

        };
        // 添加按钮点击事件
        $('#menus-btn-add').click(function () {
            showEditModel();
        });
        // 搜索按钮点击事件
        $('#menus-btn-search').click(function () {
            var keyword = $('#menus-edit-search').val();
            table.reload('platform-table', {where: {userna: keyword}});
        });
    });
</script>